const { getEssay, 
  createEssay, 
  destroyEssay, 
  getEssayUp, 
  getEssayType, 
  getEssayAll, 
  updateEssay, 
  changeStatusEssay, 
  typeisExit, 
  getEssayCount } = require('../service/essay'),
  { updateToQiniu, returnInfo } = require('../lib/utils'),
  { nanoid } = require('nanoid'),
  { IMAGE_UPLOAD, API } = require('../config/err_config'),
  { qiniu } = require('../config/config');
const path = require('path')

class Essay {
  async getEssay(ctx, next) {
    try {
      const { tid, offset, limit = 10 } = ctx.request.body
      let returnMess = await getEssay(tid, Number(offset), Number(limit));
      ctx.body = returnInfo(API.GET_ESSAY_SUCCESS, returnMess);
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.GET_ESSAY_ERROR)
    }

  }
  async getEssayUp(ctx, next) {
    try {
      const { offset, limit = 5 } = ctx.request.body
      let returnMess = await getEssayUp(Number(offset), Number(limit));
      ctx.body = returnInfo(API.GET_ESSAY_SUCCESS, returnMess);
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.GET_ESSAY_ERROR)
    }

  }
  async getEssayType(ctx, next) {
    try {
      const { offset, limit = 5, typeid } = ctx.request.body
      let returnMess = await getEssayType(Number(offset), Number(limit), typeid);
      ctx.body = returnInfo(API.GET_ESSAY_SUCCESS, returnMess);
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.GET_ESSAY_ERROR)
    }

  }
  async getEssayAll(ctx) {
    try {
      let returnMess = await getEssayAll();
      ctx.body = returnInfo(API.GET_ESSAY_SUCCESS, returnMess);
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.GET_ESSAY_ERROR)
    }
  }
  async getEssayCount(ctx) {
    try {
      let count = await getEssayCount();
      ctx.body = returnInfo(API.GET_ESSAY_COUNT_SUCCESS, count[0].dataValues.count);
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.GET_ESSAY_COUNT_ERROR)
    }
  }
  async createEssay(ctx, next) {
    try {
      let essayData = ctx.request.body;
      let returnMess = await createEssay(essayData);
      ctx.body = returnMess ? returnInfo(API.CREATE_ESSAY_SUCCESS)
        : returnInfo(API.CREATE_ESSAY_ERROR)
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.CREATE_ESSAY_ERROR)
    }


  }
  async destroyEssay(ctx) {
    let { id } = ctx.request.body;
    try {
      let returnMess = await destroyEssay(id);
      ctx.body = returnMess ? returnInfo(API.DESTROY_ESSAY_SUCCESS)
        : returnInfo(API.DESTROY_ESSAY_ERROR)
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.DESTROY_ESSAY_ERROR)
    }


  }
  async updateEssay(ctx) {
    try {
      let { id } = ctx.request.body;
      let essayData = ctx.request.body;
      let returnMess = await updateEssay(id, essayData);
      ctx.body = returnMess ? returnInfo(API.UPDATE_ESSAY_SUCCESS)
        : returnInfo(API.UPDATE_ESSAY_ERROR)
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.UPDATE_ESSAY_ERROR)
    }

  }
  async changeStatusEssay(ctx) {
    try {
      let { id, status } = ctx.request.body;
      let returnMess = await changeStatusEssay(id, status);
      ctx.body = returnMess ? returnInfo(API.CHANGE_STATUS_ESSAY_SUCCESS)
        : returnInfo(API.CHANGE_STATUS_ESSAY_ERROR)
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.CHANGE_STATUS_ESSAY_ERROR)
    }

  }
  async typeIsExit(ctx) {
    try {
      let { typeid } = ctx.request.body;
      let count = await typeisExit(typeid);
      return count > 0 ? true : false;
    } catch (error) {
      console.log(error);
      ctx.body = returnInfo(API.SELECT_TYPEID_ISEXIT_ERROR)
    }

  }
  uploadImage(ctx, next) {
    // 这是单文件上传
    // const file = ctx.request.files.qqq;
    // console.log(file.path);
    // let extname = path.extname(file.name);
    // let reader = fs.createReadStream(file.path);
    // let writer = fs.createWriteStream(
    //   `static/${Math.random()
    //     .toString(36)
    //     .substr(2)}${extname}`
    //     );
    //     reader.pipe(writer);
    //这是多文件上传    
    // const files = Object.entries(ctx.request.files);

    // files.map((item,index)=>{
    //   console.log(item[0],item[1]);
    //   let extname = path.extname(item[1].name);
    //   let reader = fs.createReadStream(item[1].path);
    //   let writer = fs.createWriteStream(
    //     `static/${Math.random()
    //       .toString(36)
    //       .substr(2)}${extname}`

    //   );
    //   reader.pipe(writer);
    //     })
    try {
      const files = Object.entries(ctx.request.files);
      let imgUrlAr = [];
      files.map((item) => {
        let extname = path.extname(item[1].name);
        let filePath = item[1].path;
        let fileName = `essayphoto${nanoid()}${extname}`;
        updateToQiniu(filePath, fileName);
        imgUrlAr.push(`${qiniu.bucket.blogsimg.domain}/${fileName}`)
      })

      if (imgUrlAr.length <= 0) {
        ctx.body = IMAGE_UPLOAD.UPLOAD_ERROR;
      }
      ctx.body =
      {
        // errno 即错误代码，0 表示没有错误。
        //       如果有错误，errno != 0，可通过下文中的监听函数 fail 拿到该错误码进行自定义处理
        "errno": 0,

        // data 是一个数组，返回图片的线上地址
        "data": imgUrlAr
      }
    } catch (error) {
      console.log(error);
      ctx.body = IMAGE_UPLOAD.UPLOAD_ERROR;
    }


  }
}
module.exports = new Essay();